import { stringify } from 'qs';
import request from '../utils/request';
import { USER_HOST, DATA_HOST, UPTOKEN_HOST, HOST } from '../utils/setting';
import G from '../utils/g';

export async function queryProjectNotice() {
  return request('/api/project/notice');
}



export async function queryRule(params) {
  return request(`/api/rule?${stringify(params)}`);
}

export async function removeRule(params) {
  return request('/api/rule', {
    method: 'POST',
    body: {
      ...params,
      method: 'delete',
    },
  });
}

export async function addRule(params) {
  return request('/api/rule', {
    method: 'POST',
    body: {
      ...params,
      method: 'post',
    },
  });
}

export async function fakeSubmitForm(params) {
  return request('/api/forms', {
    method: 'POST',
    body: params,
  });
}

export async function fakeChartData() {
  return request('/api/fake_chart_data');
}

export async function queryTags() {
  return request('/api/tags');
}

export async function queryBasicProfile() {
  return request('/api/profile/basic');
}

export async function queryAdvancedProfile() {
  return request('/api/profile/advanced');
}

export async function queryFakeList(params) {
  return request(`/api/fake_list?${stringify(params)}`);
}

export async function fakeAccountLogin(params) {
  return request('/api/login/account', {
    method: 'POST',
    body: params,
  });
}

export async function fakeRegister(params) {
  return request('/api/register', {
    method: 'POST',
    body: params,
  });
}

export async function queryNotices() {
  return request('/api/notices');
}

//用户登录
export async function accountLogin(params) {

  /*
  * 此处以application/x-www-form-urlencoded;charset=UTF-8方式
  * 参数POST中就以key=value&key=value形式
  * */
  return request(`${USER_HOST}/auth/token`, {
    method: 'POST',
    headers : {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    },
    body: stringify(params)
  });
}


//获取登录用户角色
export async function getRole(params){
  return request(`${DATA_HOST}/dashboard/get-role/`);
}

//用户退出
export async function accountLogout(params) {
  return request(`/logout`);
}

//获取七牛Token
export async function queryQiniuToken(params) {
  return request(`${UPTOKEN_HOST}/api/fileUpload/findToken`);
}

//获取商品分类
export async function queryGoodsCategory(params){
  return request(`${DATA_HOST}/goods/category/`);
}

//获取商品分组
export async function getGoodsGroup(params){
  return request(`${DATA_HOST}/dashboard/goods/group/`);
}

//商品分组新增
export async function addGoodsGroup(params){
  return request(`${DATA_HOST}/dashboard/goods/group/save/`, {
    method: 'POST',
    body: {
      ...params
    }
  });
}

//商品分组
export async function goodsGroup(params){
  return request(`${DATA_HOST}/dashboard/goods/change-groups/?${stringify(params)}`);
}

//商品复制

export async function goodsCopy(params){
  return request(`${DATA_HOST}/dashboard/goods/copy/?${stringify(params)}`);
}

//商品保存
export async function saveGoods(params){
  return request(`${DATA_HOST}/dashboard/goods/save/`, {
    method: 'POST',
    body: {
      ...params
    }
  });
}

//商品列表
export async function goodsList(params) {
  return request(`${DATA_HOST}/dashboard/goods/?${stringify(params)}`);
}

//修改商品状态
export async function changeStatus(params) {
  return request(`${DATA_HOST}/dashboard/goods/change-status/?${stringify(params)}`);
}


//商品详情
export async function getGoodsDetail(params) {
  return request(`${DATA_HOST}/dashboard/goods/${params.id}`);
}

//订单列表
export async function orderList(params) {
  return request(`${DATA_HOST}/dashboard/order/?${stringify(params)}`)
}

//各种订单状态数量
export async function statusCount(params) {
  return request(`${DATA_HOST}/dashboard/order/base-count/?${stringify(params)}`)
}

//订单详情
export async function orderDetail(params) {
  return request(`${DATA_HOST}/dashboard/order/${params.id}`)
}

//修改订单信息
export async function changeOrderInfo(params) {
  return request(`${DATA_HOST}/dashboard/order/change-info/?${stringify(params)}`)
}

//发货
export async function orderSendGoods(params){
  return request(`${DATA_HOST}/dashboard/order/provide/`, {
    method: 'POST',
    headers : {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    },
    body: stringify(params)
  })
}

//维权列表
export async function refundList(params) {
  return request(`${DATA_HOST}/dashboard/order/refund-list/?${stringify(params)}`)
}

//维权详情
export async function refundDetail(params) {
  return request(`${DATA_HOST}/dashboard/order/refund-detail/?${stringify(params)}`)
}

//确认退款
export async function actionRefund(params) {
  return request(`${DATA_HOST}/dashboard/order/confirm-refund/`, {
    method: 'POST',
    body: {
      ...params
    }
  })
}

//拒绝退款

export async function actionRefused(params) {
  return request(`${DATA_HOST}/dashboard/order/refuse-refund/`, {
    method: 'POST',
    headers : {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    },
    body: stringify(params)
  })
}

//财确认打款
export async function actionRefundSuccess(params) {
  return request(`${DATA_HOST}/dashboard/order/refund-success/`, {
    method: 'POST',
    headers : {
      'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    },
    body: stringify(params)
  })
}

//商家退货地址
export async function getAddressList(params){
  return request(`${DATA_HOST}/dashboard/user/business-address-list/`);
}

//获取物流公司
export async function expressCompany(params){
  return request(`${DATA_HOST}/express/list`);
}

//导出订单列表
export async function exportOrder(parmas){
  return request(`${DATA_HOST}/excel/exportOrderInfo`);
}

//获取微信小程序二维码
export async function getQrcode(params) {
  return request(`${DATA_HOST}/dashboard/weixin/qr-code/`, {
    method: 'POST',
    body: {
      ...params
    }
  })
}

//折扣新增
export async function saveDiscount(params) {
  const url = params.id ? `${DATA_HOST}/dashboard/promotion/update/${params.id}` : `${DATA_HOST}/dashboard/promotion/create/`;
  return request(url, {
    method: 'POST',
    body: {
      ...params
    }
  })
}

//促销详情
export async function getDiscountDetail(params){
  return request(`${DATA_HOST}/dashboard/promotion/detail/${params.id}`)
}

//折扣列表
export async function discountList(params){
  return request(`${DATA_HOST}/dashboard/promotion/?${stringify(params)}`)
}

//提前结束优惠券
export async function finishDiscount(params){
  return request(`${DATA_HOST}/dashboard/promotion/finish/${params.id}`)
}

//删除优惠券
export async function deleteDiscount(params){
  return request(`${DATA_HOST}/dashboard/promotion/delete/${params.id}`)
}


//折扣统计数据
export async function getDiscountGoods(params){
  return request(`${DATA_HOST}/dashboard/promotion/data?${stringify(params)}`)
}


//优惠券新增
export async function saveCoupon(params) {
  const url = params.id ? `${DATA_HOST}/dashboard/coupon/update/${params.id}` : `${DATA_HOST}/dashboard/coupon/create/`;
  return request(url, {
    method: 'POST',
    body: {
      ...params
    }
  })
}
//优惠券列表
export async function couponList(params){
  return request(`${DATA_HOST}/dashboard/coupon/?${stringify(params)}`)
}

//优惠券详情
export async function getCouponDetail(params){
  return request(`${DATA_HOST}/dashboard/coupon/detail/${params.id}`)
}

//提前结束优惠券
export async function finishCoupon(params){
  return request(`${DATA_HOST}/dashboard/coupon/finish/${params.id}`)
}

//删除优惠券
export async function deleteCoupon(params){
  return request(`${DATA_HOST}/dashboard/coupon/delete/${params.id}`)
}


//优惠券商品数据
export async function getCouponGoods(params){
  return request(`${DATA_HOST}/dashboard/coupon/goods-list/${params.id}`)
}


//用户列表
export async function userList(params){
  return request(`${DATA_HOST}/dashboard/user/get-users/?${stringify(params)}`)
}


//用户分组列表
export async function userGroupList(params){
  return request(`${DATA_HOST}/dashboard/user/groups-list/?${stringify(params)}`)
}

//用户分组新增
export async function addUserGroup(params){
  return request(`${DATA_HOST}/dashboard/user/group/save/`, {
    method: 'POST',
    body: {
      ...params
    }
  });
}

//用户分组
export async function userGroup(params){
  return request(`${DATA_HOST}/dashboard/user/change-groups/?${stringify(params)}`);
}


//用户概况
export async function userGeneral(params){
  return request(`${DATA_HOST}/dashboard/user/general/?${stringify(params)}`)
}

//用户省份
export async function userProvince(params){
  return request(`${DATA_HOST}/dashboard/user/province-general/?${stringify(params)}`)
}

//用户男女比例
export async function userSex(params){
  return request(`${DATA_HOST}/dashboard/user/sex-general/?${stringify(params)}`)
}


//首页销售数据
export async function getSalesData(params){
  return request(`${DATA_HOST}/dashboard/statistics/sales-base/?${stringify(params)}`)
}

//首页访问数据
export async function getPvData(params){
  return request(`${DATA_HOST}/dashboard/statistics/pv/?${stringify(params)}`)
}

//首页支付笔数
export async function getPaymentData(params){
  return request(`${DATA_HOST}/dashboard/statistics/payments-base/?${stringify(params)}`)
}

//首页用户活跃
export async function getUserActiveData(params){
  return request(`${DATA_HOST}/dashboard/statistics/user-activity/?${stringify(params)}`)
}

//订单图表
export async function getOrderChartData(params){
  return request(`${DATA_HOST}/dashboard/statistics/order/?${stringify(params)}`)
}


//订单概况
export async function getOrderGeneral(params){
  return request(`${DATA_HOST}/dashboard/statistics/order-base/?${stringify(params)}`)
}


//首页banner管理
export async function indexBanner(params){
  return request(`${DATA_HOST}/dashboard/home/banner-list/?${stringify(params)}`)
}

//首页banner新增/修改
export async function saveIndexBanner(params){
  if(params.id){
    return request(`${DATA_HOST}/dashboard/home/banner-change/`, {
      method: 'POST',
      headers : {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      },
      body: stringify(params)
    });
  }else{
    return request(`${DATA_HOST}/dashboard/home/banner-save/`, {
      method: 'POST',
      body: {
        ...params
      }
    })
  }
}

//首页Banner详情
export async function indexBannerDetail(params){
  return request(`${DATA_HOST}/dashboard/home/banner-detail/?${stringify(params)}`)
}

//首页Banner删除
export async function deleteIndexBanner(params){
  return request(`${DATA_HOST}/dashboard/home/banner-delete/?${stringify(params)}`)
}

//首页分组管理
export async function indexGroup(params){
  return request(`${DATA_HOST}/dashboard/home/group-list/?${stringify(params)}`)
}


//首页分组新增/修改
export async function saveIndexGroup(params){
  if(params.id){
    return request(`${DATA_HOST}/dashboard/home/group-change/`, {
      method: 'POST',
      headers : {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      },
      body: stringify(params)
    });
  }else{
    return request(`${DATA_HOST}/dashboard/home/group-save/`, {
      method: 'POST',
      body: {
        ...params
      }
    })
  }
}
//首页Group详情
export async function indexGroupDetail(params){
  return request(`${DATA_HOST}/dashboard/home/group-detail/?${stringify(params)}`)
}

//首页Group删除
export async function deleteIndexGroup(params){
  return request(`${DATA_HOST}/dashboard/home/group-delete/?${stringify(params)}`)
}

//首页分组商品管理
export async function indexGroupGoods(params){
  return request(`${DATA_HOST}/dashboard/home/group-goods-list/?${stringify(params)}`)
}

//首页分组商品移除
export async function deleteIndexGroupGoods(params){
  return request(`${DATA_HOST}/dashboard/home/group-goods-remove/`, {
      method: 'POST',
      body: {
        ...params
      }
    })
}



//首页分组商品新增
export async function saveAddIndexGroupGoods(params){
  return request(`${DATA_HOST}/dashboard/home/group-goods-add/`, {
      method: 'POST',
      body: {
        ...params
      }
    })
}

//首页商品分组修改
export async function saveIndexGroupGoods(params){
  return request(`${DATA_HOST}/dashboard/home/group-goods-change/`, {
      method: 'POST',
      headers : {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      },
      body: stringify(params)
    });
}

//漏斗
export async function getFilterData(params){
  return request(`${DATA_HOST}/dashboard/statistics/pv-info/?${stringify(params)}`)
}

//商品PV排行
export async function getGoodsPvData(params){
  return request(`${DATA_HOST}/dashboard/statistics/goods-pv-ranking/?${stringify(params)}`)
}

//商品销量排行
export async function getGoodsPayData(params){
  return request(`${DATA_HOST}/dashboard/statistics/goods-sales-ranking/?${stringify(params)}`)
}


//累计粉丝数和访问粉丝数
export async function getFansChartData(params){
  return request(`${DATA_HOST}/dashboard/statistics/fans-info-detail/?${stringify(params)}`)
}

//queryActivityDetail活动详情
export async function queryActivityDetail(params){
  return request(`${DATA_HOST}/dashboard/activity/detail/?${stringify(params)}`)
}

//活动列表
export async function queryActivities(params) {
  return request(`${DATA_HOST}/dashboard/activity/?${stringify(params)}`);
}

//saveActivity
export async function saveActivity(params){
  return request(`${DATA_HOST}/dashboard/activity/save/`, {
      method: 'POST',
      body: {
      ...params
    }
  });
}

//changeActivityStatus 活动状态
export async function changeActivityStatus(params) {
  return request(`${DATA_HOST}/dashboard/activity/change/?${stringify(params)}`);
}

//deleteActivity
export async function deleteActivity(params) {
  return request(`${DATA_HOST}/dashboard/activity/delete/?${stringify(params)}`);
}

//数据概览，基础数据
export async function getBaseAnalysis(params) {
  return request(`${DATA_HOST}/dashboard/statistics/base-info/?${stringify(params)}`);
}


//新增商品时获取相关商品getAddLinkGoods
export async function getAddLinkGoods(params) {
  return request(`${DATA_HOST}/dashboard/goods/get-link-goods/?${stringify(params)}`);
}


//获取商家列表
export async function shopList(params) {
  return request(`${DATA_HOST}/dashboard/user/business-list/?${stringify(params)}`);
}

//新增商家
export async function addBusiness(params){
  let data = new FormData();
  for(let k in params){
    if(params[k]){
      data.append(k,params[k]);
    }
  }
  return request(`${USER_HOST}/user/create`, {
    method: 'POST',
    headers : {
      'Content-Type': 'multipart/form-data',  //fromData请求是不需要传content-type,浏览器会自动添加,request里判断会去掉
      'X-APIHitgou-ClientId': window.localStorage.clientId,
      'X-APIHitgou-AccessToken': window.localStorage.token
    },
    body: data
  });
}

//设置商家

export async function setBusiness(params){
  return request(`${DATA_HOST}/dashboard/user/set-business/?${stringify(params)}`);
}


//待结算商家列表
export async function getUnPayBusinessList(params){
  return request(`${DATA_HOST}/dashboard/settlement/business-list/?${stringify(params)}`);
}


// 已结算列表
export async function getPayedBusinessList(params){
  return request(`${DATA_HOST}/dashboard/settlement/batch-list/?${stringify(params)}`);
}

// 待结算商家SKU
export async function getUnPaySku(params){
  return request(`${DATA_HOST}/dashboard/settlement/goods-list/?${stringify(params)}`);
}

//获取商家未结算订单列表
export async function getBusinessOrderList(params){
  return request(`${DATA_HOST}/dashboard/settlement/order-list/?${stringify(params)}`);
}


// 生成结算批次号

export async function postCreateBusinessPay(params){
  return request(`${DATA_HOST}/dashboard/settlement/submit-order/`, {
    method: 'POST',
    body: {
      ...params
    }
  });
}


// 图片上传
export async function postFileUpload(params){
  return request(`${HOST}/fs/upload/img`, {
    headers : {
      'Content-Type': 'multipart/form-data',  //fromData请求是不需要传content-type,浏览器会自动添加,request里判断会去掉
    },
    method: 'POST',
    body: params
  });
}

//  视频上传
export async function postFileVideoUpload(params){
  return request(`${HOST}/fs/upload/video`, {
    headers : {
      'Content-Type': 'multipart/form-data',  //fromData请求是不需要传content-type,浏览器会自动添加,request里判断会去掉
    },
    method: 'POST',
    body: params
  });
}

// 获取商家需要结算的维权订单
export async function getBusinessRefundOrderList(params){
  return request(`${DATA_HOST}/dashboard/settlement/refund-order-list/?${stringify(params)}`);
}


// 获取商品绑定的文章
export async function getBindArticleList(params){
  return request(`${HOST}/api/article/findByGoodsId?${stringify(params)}`);
}

// 商品解绑文章
export async function getUnbindArticle({articleId, goodsId}){
  return request(`${HOST}/api/article/unbindGoods/${articleId}/${goodsId}`);
}

// 搜索文章
export async function searchArticle(params){
  return request(`${HOST}/api/article/findList?${stringify(params)}`);
}

//商品绑定文章
export async function getBindArticle(params){
  return request(`${HOST}/api/article/bindGoods?${stringify(params)}`);
}